import { createHmac } from 'node:crypto'

export default defineEventHandler(async (event) => {
  const headers = getHeaders(event)
  const userId = headers['user-id'] || ''
  const companyId = headers['company-id'] || ''

  // 请求体生成特征码
  const method = event.method.toUpperCase()
  const path = getRequestURL(event).pathname
  const reqData = method === 'GET' ? getQuery(event) : await readBody(event)
  const attributeData = JSON.stringify({ userId, companyId, method, path, reqData })
  const attributeCode = createHmac('sha1', 'secret').update(attributeData).digest('hex')
  event.context.attributeCode = attributeCode
})
